package com.zzstm.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzstm.commonUtils.JwtUtils;
import com.zzstm.commonUtils.R;
import com.zzstm.commonUtils.UcenterMemberCommon;
import com.zzstm.eduservice.client.UcenterClient;
import com.zzstm.eduservice.entity.EduComment;
import com.zzstm.eduservice.service.EduCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-11-19
 */
@RestController
@RequestMapping("/eduservice/comment")
@CrossOrigin
public class EduCommentController {

    @Autowired
    private EduCommentService commentService;

    @Autowired
    private UcenterClient ucenterClient;

    //根据课程id分页查询评论
    @GetMapping("/getPageComment/{pageNum}/{pageSize}/{courseId}")
    public R getPageComment(@PathVariable long pageNum,@PathVariable long pageSize,@PathVariable String courseId){
        Page<EduComment> page = new Page<>(pageNum,pageSize);
        Map<String,Object> map = commentService.getPageComment(page,courseId);
        return R.ok().data("map",map);
    }

    //添加评论
    @PostMapping("/addComment")
    public R addComment(@RequestBody EduComment comment, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)){
            return R.error().message("请先登录");
        }
        comment.setMemberId(memberId);
        UcenterMemberCommon memberCommon = ucenterClient.getUserInfoById(memberId);
        comment.setAvatar(memberCommon.getAvatar());
        comment.setNickname(memberCommon.getNickname());
        commentService.save(comment);
        return R.ok();
    }

}

